package com.blue.http.config;

import com.blue.http.NettyHttpServer;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 * HttpServer解析
 *
 * @author Jin Zheng
 * @date 2018-10-26
 */
public class HttpServerParser extends AbstractSimpleBeanDefinitionParser
{
	public HttpServerParser()
	{
	}

	@Override
	protected Class<?> getBeanClass(Element element)
	{
		return NettyHttpServer.class;
	}

	@Override
	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
	{
		try
		{
			builder.addPropertyValue("port", element.getAttribute("port"));

			this.parseRef(element, builder, "taskExecutor", "task-executor");
			this.parseRef(element, builder, "httpConfig", "http-config");
			this.parseRef(element, builder, "websocketConfig", "websocket-config");
			this.parseRef(element, builder, "sslConfig", "ssl-config");

		}
		catch (Exception e)
		{
			parserContext.getReaderContext().error(NettyHttpServer.class.getName() + " 无法创建", element, null, e);
		}
	}

	private void parseRef(Element element, BeanDefinitionBuilder builder, String property, String attribute)
	{
		String ref = element.getAttribute(attribute);
		if (ref != null && !ref.isEmpty())
		{
			builder.addPropertyValue(property, new RuntimeBeanReference(ref));
		}
	}
}
